{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## _*The EOH (Evolution of Hamiltonian) Algorithm*_\n",
    "\n",
    "This notebook demonstrates how to use the `Qiskit Aqua` library to invoke the EOH algorithm and process the result.\n",
    "\n",
    "Further information may be found for the algorithms in the online [Aqua documentation](https://qiskit.org/documentation/apidoc/qiskit.aqua.algorithms.html).\n",
    "\n",
    "For this particular demonstration, we illustrate the `EOH` algorithm. First, two `WeightedPauliOperator` instances we created are randomly generated Hamiltonians."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from qiskit import BasicAer\n",
    "from qiskit.transpiler import PassManager\n",
    "from qiskit.aqua import QuantumInstance\n",
    "from qiskit.aqua.operators import MatrixOperator, op_converter\n",
    "from qiskit.aqua.algorithms import EOH\n",
    "from qiskit.aqua.components.initial_states import Custom\n",
    "\n",
    "num_qubits = 2\n",
    "temp = np.random.random((2 ** num_qubits, 2 ** num_qubits))\n",
    "qubit_op = op_converter.to_weighted_pauli_operator(MatrixOperator(matrix=temp + temp.T))\n",
    "temp = np.random.random((2 ** num_qubits, 2 ** num_qubits))\n",
    "evo_op = op_converter.to_weighted_pauli_operator(MatrixOperator(matrix=temp + temp.T))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For EOH, we would like to evolve some initial state (e.g. the uniform superposition state) with `evo_op` and do a measurement using `qubit_op`. Below, we illustrate how such an example dynamics process can be easily prepared."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "evo_time = 1\n",
    "num_time_slices = 1\n",
    "state_in = Custom(qubit_op.num_qubits, state='uniform')\n",
    "eoh = EOH(qubit_op, state_in, evo_op, evo_time=evo_time, num_time_slices=num_time_slices)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can then configure the quantum backend and execute our `EOH` instance:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The result is\n",
      "{'avg': (3.3315538738820436-1.1573250579094587e-16j), 'std_dev': 0.0}\n"
     ]
    }
   ],
   "source": [
    "backend = BasicAer.get_backend('statevector_simulator')\n",
    "quantum_instance = QuantumInstance(backend)\n",
    "\n",
    "ret = eoh.run(quantum_instance)\n",
    "print('The result is\\n{}'.format(ret))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
